package com.fjec.compensate.rabbitmq.consumer;

import com.fjec.common.Const;
import com.fjec.common.protocol.FjecFruitsProto;
import com.fjec.compensate.service.IBizMqCompensateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.slf4j.MDC;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
public class CompensateConsumer {

    @Resource
    private IBizMqCompensateService mqCompensateService;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(
                            value = "${fjec.compensate.delay.queue.name}",
                            durable = "${fjec.compensate.delay.queue.durable}"
                    ),
                    exchange = @Exchange(
                            value = "${fjec.compensate.delay.exchange.name}",
                            durable = "${fjec.compensate.delay.exchange.durable}",
                            type = "${fjec.compensate.delay.exchange.type}",
                            ignoreDeclarationExceptions = "${fjec.compensate.delay.exchange.ignoreDeclarationExceptions}"
                    ),
                    key = "${fjec.compensate.delay.router.key}"
            )
    )
    @RabbitHandler
    public void onReceiveDelayMessage(Message message, Channel channel, @Headers Map<String, Object> headers) throws Exception {
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        Long dataId = MapUtils.getLong(headers, Const.MQ_HEADER_DATA_ID_KEY);
        Integer msgType = MapUtils.getInteger(headers, Const.MQ_HEADER_MSG_TYPE_KEY);
        boolean result = mqCompensateService.handleDelayMessage(dataId, msgType, message);
        if (result) {
            channel.basicAck(deliveryTag, true);
        } else {
            channel.basicReject(deliveryTag, false);
        }
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(
                            value = "${fjec.compensate.complete.queue.name}",
                            durable = "${fjec.compensate.complete.queue.durable}"
                    ),
                    exchange = @Exchange(
                            value = "${fjec.compensate.complete.exchange.name}",
                            durable = "${fjec.compensate.complete.exchange.durable}",
                            type = "${fjec.compensate.complete.exchange.type}",
                            ignoreDeclarationExceptions = "${fjec.compensate.complete.exchange.ignoreDeclarationExceptions}"
                    ),
                    key = "${fjec.compensate.complete.router.key}"
            )
    )
    @RabbitHandler
    public void onReceiveCompleteMessage(Message message, Channel channel, @Headers Map<String, Object> headers) throws Exception {
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        Long dataId = MapUtils.getLong(headers, Const.MQ_HEADER_DATA_ID_KEY);
        Integer msgType = MapUtils.getInteger(headers, Const.MQ_HEADER_MSG_TYPE_KEY);
        boolean result = mqCompensateService.handleCompleteMessage(dataId, msgType, message);
        if (result) {
            channel.basicAck(deliveryTag, true);
        } else {
            channel.basicReject(deliveryTag, false);
        }
    }
}
